package org.xxpay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.UserLocale;
import org.xxpay.core.service.IUserLocaleService;
import org.xxpay.service.mapper.UserLocaleMapper;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户语言偏好服务实现类
 * 
 * @author xxpay
 */
@Service
public class UserLocaleServiceImpl extends ServiceImpl<UserLocaleMapper, UserLocale> implements IUserLocaleService {

    // 支持的语言列表
    private static final List<String> SUPPORTED_LOCALES = Arrays.asList(
        UserLocale.LOCALE_ZH_CN,
        UserLocale.LOCALE_EN_US
    );

    // 默认语言
    private static final String DEFAULT_LOCALE = UserLocale.LOCALE_ZH_CN;

    @Override
    public String getUserLocale(Long userId, Byte userType) {
        if (userId == null || userType == null) {
            return DEFAULT_LOCALE;
        }

        LambdaQueryWrapper<UserLocale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserLocale::getUserId, userId)
                   .eq(UserLocale::getUserType, userType);

        UserLocale userLocale = this.getOne(queryWrapper);
        
        if (userLocale != null && isSupportedLocale(userLocale.getLocale())) {
            return userLocale.getLocale();
        }

        return DEFAULT_LOCALE;
    }

    @Override
    public boolean setUserLocale(Long userId, Byte userType, String locale) {
        if (userId == null || userType == null || locale == null) {
            return false;
        }

        if (!isSupportedLocale(locale)) {
            return false;
        }

        // 查询是否已存在
        LambdaQueryWrapper<UserLocale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserLocale::getUserId, userId)
                   .eq(UserLocale::getUserType, userType);

        UserLocale existingUserLocale = this.getOne(queryWrapper);

        if (existingUserLocale != null) {
            // 更新现有记录
            existingUserLocale.setLocale(locale);
            existingUserLocale.setUpdateTime(new Date());
            return this.updateById(existingUserLocale);
        } else {
            // 创建新记录
            UserLocale newUserLocale = new UserLocale();
            newUserLocale.setUserId(userId);
            newUserLocale.setUserType(userType);
            newUserLocale.setLocale(locale);
            newUserLocale.setCreateTime(new Date());
            newUserLocale.setUpdateTime(new Date());
            return this.save(newUserLocale);
        }
    }

    @Override
    public boolean removeUserLocale(Long userId, Byte userType) {
        if (userId == null || userType == null) {
            return false;
        }

        LambdaQueryWrapper<UserLocale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserLocale::getUserId, userId)
                   .eq(UserLocale::getUserType, userType);

        return this.remove(queryWrapper);
    }

    @Override
    public boolean isSupportedLocale(String locale) {
        return locale != null && SUPPORTED_LOCALES.contains(locale);
    }

    /**
     * 获取支持的语言列表
     * 
     * @return 支持的语言列表
     */
    public List<String> getSupportedLocales() {
        return SUPPORTED_LOCALES;
    }

    /**
     * 获取默认语言
     * 
     * @return 默认语言
     */
    public String getDefaultLocale() {
        return DEFAULT_LOCALE;
    }

    /**
     * 批量设置用户语言偏好
     * 
     * @param userLocales 用户语言偏好列表
     * @return 是否设置成功
     */
    public boolean batchSetUserLocale(List<UserLocale> userLocales) {
        if (userLocales == null || userLocales.isEmpty()) {
            return false;
        }

        // 验证所有语言是否支持
        for (UserLocale userLocale : userLocales) {
            if (!isSupportedLocale(userLocale.getLocale())) {
                return false;
            }
        }

        return this.saveOrUpdateBatch(userLocales);
    }

    /**
     * 根据用户类型获取所有用户的语言偏好
     * 
     * @param userType 用户类型
     * @return 用户语言偏好列表
     */
    public List<UserLocale> getUserLocalesByType(Byte userType) {
        if (userType == null) {
            return null;
        }

        LambdaQueryWrapper<UserLocale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserLocale::getUserType, userType);

        return this.list(queryWrapper);
    }

    /**
     * 统计各语言的用户数量
     * 
     * @param userType 用户类型，为null时统计所有类型
     * @return 语言统计信息
     */
    public List<UserLocale> getLocaleStatistics(Byte userType) {
        // 这里可以使用自定义SQL查询来实现统计功能
        // 由于MyBatis-Plus的限制，这里简化处理
        LambdaQueryWrapper<UserLocale> queryWrapper = new LambdaQueryWrapper<>();
        if (userType != null) {
            queryWrapper.eq(UserLocale::getUserType, userType);
        }
        
        return this.list(queryWrapper);
    }
}
